﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace 税控接口.Common
{
    class cDll
    {
        public static void checkVer(string filename)
        {
            //string dbdllver = cUpdate.getdbVer(filename);
            string dllver = cDll.GetDllVersion(filename);
            DateTime dDllVer = DateTime.Parse(dllver);
            //DateTime dDBDllVer = DateTime.Parse(dbdllver);
            var dDBDllVer = DateTime.Now;
            if (dDllVer > dDBDllVer)
            {
                //cUpdate.savefile2DB(filename, dllver);
            }
            else
            {
                #region 取消下载
                //if (dDllVer < dDBDllVer)
                //{
                //    Common.cUpdate.getfileFromDB(filename);
                //}
                #endregion
            }
        }


        /// <summary>
        /// 提取用友需要使用的空间
        /// </summary>
        /// <returns></returns>
        public static Control getControl(string filename, string cMenu)
        {
            string path = Application.StartupPath + "\\" + filename;
            return (Control)cDll.invoke(path, filename,
                "cStart", "GetContrl", new object[] {});
        }


        /// <summary>
        /// 取得控件的版本号
        /// </summary>
        /// <param name="pathname">路径</param>
        /// <returns>版本日期</returns>
        /// 
        public static string GetDllVersion(string filename)
        {
            //string path = Application.StartupPath + "\\" + filename;
            var path = filename;
            if (!System.IO.File.Exists(path))
            {
                return "1900-01-01";
            }
            //string path1 = path + "1";
            //try { System.IO.File.Copy(path, path1, true); }
            //catch (Exception ex) { string s = ex.Message; }

            object o = cDll.invoke(path, filename, "cStart", "GetVersion", new object[] { });
            //object o = Program.RunDll(path, filename, "cStart", "getVersion", new object[] { });
            string rvalue;
            if (o == null)
            { rvalue = "1900-01-01"; }
            else { rvalue = o.ToString(); }
            return rvalue;
        }

        #region 加载DLL文件
        /// <summary>
        /// 加载DLL文件
        /// </summary>
        /// <param name="lpFileName">文件路径</param>
        /// <param name="Namespace">命名空间</param>
        /// <param name="ClassName"></param>
        /// <param name="lpprocName">方法</param>
        /// <param name="ObjArray_Parmeter">参数</param>
        /// <returns></returns>
        public static object invoke(string lpFileName, string Namespace, string ClassName, string lpprocName, object[] ObjArray_Parmeter)
        {
            var file = new System.IO.FileStream(lpFileName, System.IO.FileMode.Open);
            var size = Convert.ToInt32(file.Length);
            var bfile = new byte[size];
            file.Read(bfile, 0, size);
            file.Close();
            file.Dispose();
            Assembly MyAssembly = Assembly.Load(bfile);
            Type[] type = MyAssembly.GetTypes();
            var types = MyAssembly.GetTypes();
            Type t = null;
            int classnamelen = ClassName.Length;
            foreach (var t1 in types)
            {
                if (t1.Name == ClassName)
                { t = t1; break; }
            }

            if (t == null)
            {
                throw new Exception("方法" + ClassName + "加载出错！");
            }
            var m = t.GetMethod(lpprocName);
            if (m == null)
            {
                throw new Exception("方法" + ClassName + "|" + lpprocName + "加载出错！");
            }
            var o = Activator.CreateInstance(t);
            return m.Invoke(o, ObjArray_Parmeter);
        }
        #endregion


        //#region 加载DLL文件--从服务器
        ///// <summary>
        ///// 加载DLL文件
        ///// </summary>
        ///// <param name="lpFileName">文件路径</param>
        ///// <param name="Namespace">命名空间</param>
        ///// <param name="ClassName"></param>
        ///// <param name="lpprocName">方法</param>
        ///// <param name="ObjArray_Parmeter">参数</param>
        ///// <returns></returns>
        //public static object invoke_DB(string lpFileName, string Namespace, string ClassName, string lpprocName, object[] ObjArray_Parmeter)
        //{
        //    var bfile = cUpdate.getDllFromDB(Namespace);
        //    Assembly MyAssembly = Assembly.Load(bfile);
        //    Type[] type = MyAssembly.GetTypes();
        //    var types = MyAssembly.GetTypes();
        //    Type t = null;
        //    int classnamelen = ClassName.Length;
        //    foreach (var t1 in types)
        //    {
        //        if (t1.Name == ClassName)
        //        { t = t1; break; }
        //    }

        //    if (t == null)
        //    {
        //        throw new Exception("方法" + ClassName + "加载出错！");
        //    }
        //    var m = t.GetMethod(lpprocName);
        //    if (m == null)
        //    {
        //        throw new Exception("方法" + ClassName + "|" + lpprocName + "加载出错！");
        //    }
        //    var o = Activator.CreateInstance(t);
        //    return m.Invoke(o, ObjArray_Parmeter);
        //}
        //#endregion
    }
}
